package com.yaoandw.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.queryparser.classic.ParseException;

import com.yaoandw.givebook.Constants;
import com.yaoandw.lucene.PageSearcher;
import com.yaoandw.lucene.RecordResult;
import com.yaoandw.lucene.Result;
import com.yaoandw.util.StringUtil;

/**
 * Servlet implementation class SearchServlet
 */
public class SearchServlet extends HttpServlet {
	private static Log logger = LogFactory.getLog(SearchServlet.class); 
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SearchServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String query = request.getParameter("query");
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		if(StringUtil.isEmpty(pageNo))
			pageNo = "1";
		if(StringUtil.isEmpty(pageSize))
			pageSize = "10";
		logger.debug("query : " + query);
		List<RecordResult> results = new ArrayList<RecordResult>();
		double cost = 0;
		int totalCnt = 0;
		try {
			Result result = PageSearcher.getInstance(Constants.indexDir).search(query,Integer.valueOf(pageNo),Integer.valueOf(pageSize));
			if(result != null){
				totalCnt = result.getTotalCnt();
				results = result.getResults();
				cost = result.getCost();
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int startDisplayPage;//分页栏，从第几页开始显示
		int endDisplayPage;//分页栏，显示到第几页
		startDisplayPage = SearcherHelper.getStartDisplayPage(Integer.valueOf(pageNo), Integer.valueOf(pageSize), totalCnt);
		endDisplayPage = SearcherHelper.getEndDisplayPage(Integer.valueOf(pageNo), Integer.valueOf(pageSize), totalCnt);
		request.setAttribute("startDisplayPage", startDisplayPage);
		request.setAttribute("endDisplayPage", endDisplayPage);
		request.setAttribute("pageNo", Integer.valueOf(pageNo));
		request.setAttribute("totalCnt", totalCnt);
		request.setAttribute("results", results);
		request.setAttribute("cost", cost);
		request.setAttribute("query", query);
		request.getRequestDispatcher("result.jsp").forward(request, response);
		return;
	}

}
